package net.rlair.sbdemo.dao.jdbctemplate;

import net.rlair.sbdemo.dao.jdbctemplate.base.LocalRowMapper;
import net.rlair.sbdemo.dao.jdbctemplate.base.Page;
import net.rlair.sbdemo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author:
 * @description:
 * @create:2019-03-25 10:01
 */
@Component
public class GuanBoDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public Page<User> findAllUser(int pageNo, int pageSize) {
        String sql = "select count(*) from t_user";
        Long total = jdbcTemplate.queryForObject(sql, Long.class);
        if (total <= 0) {
            return Page.result(total, null);
        }
        String sql2 = "select * from t_user limit ?,?";
        List<User> users = jdbcTemplate.query(sql2, new LocalRowMapper(User.class), pageNo * pageSize, pageSize);
        return Page.result(total, users);
    }
}
